import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  Query,
  Request,
} from '@nestjs/common';
import { EssayService } from './essay.service';
import { CreateEssayDto } from './dto/create-essay.dto';
import { UpdateEssayDto } from './dto/update-essay.dto';
import { AuthGuard } from '@nestjs/passport';
import { AddTagDto } from './dto/add-tag.dto';
import { ToolsService } from 'src/utils/tools.service';
import { ApiOperation, ApiTags } from '@nestjs/swagger';


@ApiTags('短文模块')  
@Controller('essay')
export class EssayController {
  constructor(private readonly essayService: EssayService) {}

  @Post('add')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  @ApiOperation({ summary: '新增', description: '新增'})  
  create(@Body() createEssayDto: CreateEssayDto, @Request() req) {
    const user = req.user;
    try {
      return this.essayService.create(createEssayDto, user.userid);
    } catch (error) {
      ToolsService.fail(error);
    }
  }

  @Post('/add/tag')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  @ApiOperation({ summary: '短文添加书签', description: '短文添加书签'}) 
  addTag(@Body() addTagDto: AddTagDto) {
    //开启事务第三步：获取事务管理器)
    try {
      return this.essayService.addTag(addTagDto);
    } catch (error) {
      ToolsService.fail(error);
    }
  }

  @Get('/hastags/:id')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  @ApiOperation({ summary: '查询已有书签', description: '查询已有书签'}) 
  async findOwnerTags(@Param('id') id: string) {
    return await this.essayService.findOwnerTags(id);
  }

  @Get('list')
  // @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  @ApiOperation({ summary: '查询短文列表', description: '查询短文列表'}) 
  findAll(
    @Query('essaytitle') essaytitle: string,
    @Query('tagId') tagId: string,
    @Query('userId') userId: string,
    @Query('pageSize') pageSize: number,
    @Query('currentPage') currentPage: number,
  ) {
    return this.essayService.findAll(
      essaytitle ? essaytitle : '',
      tagId ? tagId : '',
      {
        pageSize: pageSize ? pageSize : 100,
        currentPage: currentPage ? currentPage - 1 : 0,
      },
      userId,
    );
  }

  @Get('recycling-station/list')
  @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
  @ApiOperation({ summary: '查询短文垃圾站', description: '查询短文垃圾站'}) 
  findAllRecyclingStation(
    @Query('essaytitle') essaytitle: string,
    @Query('pageSize') pageSize: number,
    @Query('currentPage') currentPage: number,
    @Request() req,
  ) {
    return this.essayService.findAllRecyclingStation(
      essaytitle ? essaytitle : '',
      {
        pageSize: pageSize ? pageSize : 100,
        currentPage: currentPage ? currentPage - 1 : 0,
      },
      req.user.userid,
    );
  }

  @Get(':id')
  @ApiOperation({ summary: '查询单个短文', description: '查询单个短文'}) 
  findOne(@Param('id') id: string) {
    return this.essayService.findOne(id);
  }

  @Patch(':id')
  @ApiOperation({ summary: '更新单个短文', description: '更新单个短文'}) 
  update(@Param('id') id: string, @Body() updateEssayDto: UpdateEssayDto) {
    try {
      return this.essayService.update(id, updateEssayDto);
    } catch (error) {
      ToolsService.fail(error);
    }
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除单个短文', description: '删除单个短文'}) 
  remove(@Param('id') id: string) {
    try {
      return this.essayService.remove(id);
    } catch (error) {
      ToolsService.fail(error);
    }
  }

  @Post('batchRecovery')
  @ApiOperation({ summary: '批量恢复', description: '批量恢复'}) 
  batchRecovery(@Body('ids') ids: string[]) {
    try {
      return this.essayService.batchRecovery(ids);
    } catch (error) {
      ToolsService.fail(error);
    }
  }

  @Post('batchDestroy')
  @ApiOperation({ summary: '批量销毁', description: '批量销毁'}) 
  batchDestroy(@Body('ids') ids: string[]) {
    try {
      return this.essayService.batchDestroy(ids);
    } catch (error) {
      ToolsService.fail(error);
    }
  }

  @Delete('/destroy/:id')
  @ApiOperation({ summary: '单个销毁', description: '单个销毁'}) 
  destroy(@Param('id') id: string) {
    try {
      return this.essayService.destroy(id);
    } catch (error) {
      ToolsService.fail(error);
    }
  }

  @Post('/pinned/:id')
  @ApiOperation({ summary: '置顶', description: '置顶'}) 
  pinned(@Param('id') id: string,@Body('status') status: number) {
    try {
      return this.essayService.pinned(id,status);
    } catch (error) {
      ToolsService.fail(error);
    }
  }

}
